package com.ruoyi.system.service.sso;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jayway.jsonpath.JsonPath;
import com.ruoyi.system.domain.IndsvrSsoApp;
import com.ruoyi.system.domain.IndsvrSsoAppUserMap;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.HashMap;
@Component
public class NextTerminal implements SimulationLogin{
    @Override
    public Object run(IndsvrSsoApp app, IndsvrSsoAppUserMap user, HttpServletRequest request) throws Exception {
        return run(user.getUsername(),user.getPassword(),app.getBaseUrl());
    }

    public Auths run(String user, String pwd, String baseUrl) throws Exception{


        OkHttpUtils okHttpUtils = new OkHttpUtils();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("username",user);
        stringStringHashMap.put("password",pwd);
        String s = okHttpUtils.postForJson(baseUrl+"/login", new Gson().toJson(stringStringHashMap));

        String token = JsonPath.read(s, "$.data.token");

        Auths auths = new Auths();
        auths.token= token;
        return auths;
    }


    @Data
    public static class Auths{
        String token;
    }
}
